import 'package:dotted_border/dotted_border.dart';
import 'package:flutter/material.dart';
import 'package:flutter_fun_zjversion/config/resource_manager.dart';
import 'package:flutter_fun_zjversion/ui/page/splash.dart';
import 'package:flutter_fun_zjversion/ui/widget/turntable.dart';
import 'package:flutter_marquee/animation_direction.dart';
import 'package:flutter_marquee/index.dart';

class LotteryPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(
          "国庆七天乐",
          style: Theme.of(context).textTheme.title,
        ),
        backgroundColor: Colors.white,
        leading: IconButton(
            icon: Icon(
              Icons.arrow_back,
              color: Colors.grey,
            ),
            onPressed: () {}),
      ),
      body: Container(
        decoration: BoxDecoration(
            color: Colors.red,
            image: DecorationImage(
                image: AssetImage(ImageHelper.wrapAssets("bg_activity.png")),
                fit: BoxFit.cover)),
        child: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[HeadInfo("4908", "3"),
              SignInWidget(),
              SizedBox(height: 20,),
              TurntableContainerStateless(),
        ]
          ),
        ),
      ),
    );
  }
}

class TurntableContainerStateless extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return TurntableContainer();
  }
}

/// 背景颜色组装
class HeadInfo extends StatelessWidget {
  String score;
  String time;

  HeadInfo(this.score, this.time);

  @override
  Widget build(BuildContext context) {
    return new Container(
      child: Stack(
        alignment: AlignmentDirectional.center,
        children: <Widget>[
          Positioned(
            child: Image.asset(
              ImageHelper.wrapAssets("activity_text_title.png"),
              width: 250,
              height: 180,
            ),
            top: 20,
          ),
          Positioned(
            child: Image.asset(
              ImageHelper.wrapAssets("activity_elm_3.png"),
              fit: BoxFit.cover,
              width: 61,
              height: 61,
            ),
            top: 20,
            left: 20,
          ),
          Positioned(
            child: Image.asset(
              ImageHelper.wrapAssets("activity_elm_1.png"),
              fit: BoxFit.cover,
              width: 66,
              height: 46,
            ),
            top: 195,
            left: 20,
          ),
          Positioned(
            child: Image.asset(
              ImageHelper.wrapAssets("activity_elm_2.png"),
              fit: BoxFit.cover,
              width: 114,
              height: 63,
            ),
            top: 190,
            right: 1,
          ),
          Positioned(
            child: Text(
              "10月玩转积分嗨不停",
              style: TextStyle(color: Colors.red),
            ),
            top: 200,
          ),
          Positioned(
            child: Container(
              width: 153,
              height: 72,
              padding: EdgeInsets.only(left: 10, top: 7),
              decoration: BoxDecoration(
                  color: Color(0XFFFFF6E9),
                  borderRadius: BorderRadius.circular(7)),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    "我的积分",
                    style: LottertTextStyle.scorehintStyle,
                  ),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: <Widget>[
                      Text(
                        score,
                        style: LottertTextStyle.scoreStyle,
                      ),
                      SizedBox(
                        width: 2,
                      ),
                      Text(
                        "分",
                        style: LottertTextStyle.scoreunitStyle,
                      )
                    ],
                  )
                ],
              ),
            ),
            top: 250,
            left: 20,
          ),
          Positioned(
            child: Container(
              width: 153,
              height: 72,
              decoration: BoxDecoration(
                  color: Color(0XFFFFF6E9),
                  borderRadius: BorderRadius.circular(7)),
              padding: EdgeInsets.only(left: 10, top: 7),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    "今日可抽奖次数",
                    style: LottertTextStyle.scorehintStyle,
                  ),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: <Widget>[
                      Text(
                        time,
                        style: LottertTextStyle.scoreStyle,
                      ),
                      SizedBox(
                        width: 2,
                      ),
                      Text(
                        "次",
                        style: LottertTextStyle.scoreunitStyle,
                      )
                    ],
                  )
                ],
              ),
            ),
            top: 250,
            right: 20,
          )
        ],
      ),
      width: double.infinity,
      height: 338,
    );
  }
}

class SignInWidget extends StatelessWidget {
  var awardBeans = [
    new AwartBean(assetImg: "gift_medal_1.png", award: "超萌勋章"),
    new AwartBean(assetImg: "gift_integral_2.png", award: "20积分"),
    new AwartBean(assetImg: "gift_medal_1.png", award: "节日背景"),
    new AwartBean(assetImg: "gift_integral_2.png", award: "30积分"),
    new AwartBean(assetImg: "gift_box_3.png", award: "超萌勋章"),
    new AwartBean(assetImg: "gift_box_3.png", award: "超萌勋章"),
    new AwartBean(assetImg: "gift_dialog_big.png", award: "超萌勋章"),
  ];

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      decoration: BoxDecoration(
        color: Color(0XFFFFF6E9),
        borderRadius: BorderRadius.circular(9),
      ),
      padding: EdgeInsets.all(10),

      child: DottedBorder(
        color: Color(0XFFF35A25),
        borderType: BorderType.RRect,
        radius:Radius.circular(9),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            SizedBox(
              height: 10,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Image.asset(
                  ImageHelper.wrapAssets("text_elm_1.png"),
                  width: 23,
                  height: 23,
                  fit: BoxFit.cover,
                ),
                Text(
                  "签到赢好礼",
                  style: LottertTextStyle.signTitle,
                ),
                Image.asset(
                  ImageHelper.wrapAssets("text_elm_1.png"),
                  width: 23,
                  height: 23,
                  fit: BoxFit.cover,
                )
              ],
            ),
            SizedBox(
              height: 10,
            ),
            Wrap(
              direction: Axis.horizontal,
              spacing: 10,
              runSpacing: 10,
              children: _gridWidgets(),
            ),
            Container(
              margin: EdgeInsets.only(top: 20),
              decoration: BoxDecoration(
                image: DecorationImage(image: AssetImage(ImageHelper.wrapAssets("activity_btn_default.png"),),fit: BoxFit.cover),
              ),
              width: 257,
              height: 48,
              child: Center(
                child: Text("今日签到",style: TextStyle(color: Colors.white,fontSize: 20,fontWeight: FontWeight.bold),),
              ),
            )
          ],
        ),
      ),
      width: double.infinity,
      height: 328,
      margin: EdgeInsets.only(left: 20, right: 20),
    );
  }

  Widget _buildItemGird(int index, AwartBean awartBean) {
    return Container(
      decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage(ImageHelper.wrapAssets("lottery_bg.png")))),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          SizedBox(
            height: 2,
          ),
          Text(
            "第${index}天",
            style: TextStyle(fontSize: 14, color: Colors.white),
          ),
          Image.asset(
            ImageHelper.wrapAssets(awartBean.assetImg),
            width: 28,
            height: 28,
          ),
          Text(awartBean.award,
              style: TextStyle(fontSize: 14, color: Colors.white)),
          SizedBox(
            height: 2,
          ),
        ],
      ),
      width: 69,
      height: 75,
    );
  }

  Widget _buildLastItemGird() {
    return Container(
      padding: EdgeInsets.all(5),
      decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage(ImageHelper.wrapAssets("lottery_bg.png")),
            fit: BoxFit.fitWidth
          ),
        borderRadius: BorderRadius.circular(9)
      ),
      child: Row(
        children: <Widget>[
          Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text(
                "第7天",
                style: LottertTextStyle.signday,
              ),
              Text("未开始", style: LottertTextStyle.signday)
            ],
          ),
          Expanded(
              child: Image.asset(
            ImageHelper.wrapAssets('gift_dialog_big.png'),
            width: 56,
            height: 56,
            fit: BoxFit.contain,
          ))
        ],
      ),
      width: 144,
      height: 75,
    );
  }

  List<Widget> _gridWidgets(){
    var list = <Widget>[];
    for(int i=0;i<awardBeans.length;i++){
      if(i!=awardBeans.length-1){
        list.add(_buildItemGird(i,awardBeans[i]));
      }else{
        list.add(_buildLastItemGird());
      }
    }
    return list;
  }
}

class AwartBean {
  String assetImg;
  String award;

  AwartBean({this.assetImg, this.award});
}

class LottertTextStyle {
  static final TextStyle scorehintStyle =
      TextStyle(color: Color(0XFFF35A25), fontSize: 14);
  static final TextStyle scoreStyle = TextStyle(
      color: Color(0XFFF35A25), fontSize: 30, fontWeight: FontWeight.bold);
  static final TextStyle scoreunitStyle = TextStyle(
      color: Color(0XFFF35A25), fontSize: 15, fontWeight: FontWeight.w600);

  static final TextStyle signTitle =
      TextStyle(color: Color(0XFFF35A25), fontSize: 16);
  static final TextStyle signday = TextStyle(fontSize: 14, color: Colors.white);
}


class TurntableWidget extends StatefulWidget {
  @override
  TurntableWidgetState createState() => new TurntableWidgetState();
}

class TurntableWidgetState extends State<TurntableWidget>{

  var data;
  @override
  Widget build(BuildContext context) {
    List<Future> futures = [];
    for(int i=0;i<data.length;i++){
      futures.add(data[i].getImage());
    }
    return FutureBuilder(builder: (context,snapshot){
      if (snapshot.connectionState != ConnectionState.done) {
        return Text("图片加载中");
      }
      return CustomPaint(
          painter: TurntablePainter(data),
      );
    },future: Future.wait(futures),);
  }
  @override
  void initState() {

    data=[ ItemTurntableBean("text_elm_1.png","一等奖"),
      ItemTurntableBean("text_elm_1.png","二等奖"),
      ItemTurntableBean("text_elm_1.png","三等奖"),
      ItemTurntableBean("text_elm_1.png","四等奖"),
      ItemTurntableBean("text_elm_1.png","五等奖"),
      ItemTurntableBean("text_elm_1.png","六等奖"),];
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

}


class TurntableContainer extends StatefulWidget {
  @override
  State createState() => new TurntableContainerState();
}
class TurntableContainerState extends State<TurntableContainer> with TickerProviderStateMixin{
  AnimationController controller ;
  var start = 0.0;
  var end = 4.0;
  var average = 1/6;
  var curve;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print("controller.initState()");
    controller = AnimationController(duration: const Duration(seconds: 6), vsync: this);
    controller.addStatusListener((status){
      if (status == AnimationStatus.completed) {
          controller.reset();
      }
    });
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Color(0XFFFFF6E9),
        borderRadius: BorderRadius.circular(9),
      ),
      padding: EdgeInsets.all(10),

      child: DottedBorder(
        color: Color(0XFFF35A25),
        borderType: BorderType.RRect,
        radius:Radius.circular(9),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            SizedBox(
              height: 10,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Image.asset(
                  ImageHelper.wrapAssets("text_elm_1.png"),
                  width: 23,
                  height: 23,
                  fit: BoxFit.cover,
                ),
                Text(
                  "幸运轮盘",
                  style: LottertTextStyle.signTitle,
                ),
                Image.asset(
                  ImageHelper.wrapAssets("text_elm_1.png"),
                  width: 23,
                  height: 23,
                  fit: BoxFit.cover,
                )
              ],
            ),
            SizedBox(
              height: 10,
            ),
            Container(
              decoration: BoxDecoration(
                  image: DecorationImage(image: AssetImage(ImageHelper.wrapAssets("table_not_bg.png")),fit: BoxFit.fill)
              ),
              child:Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Image.asset(ImageHelper.wrapAssets("activity_notice.png"),width: 20,height: 16,),
                  Container(
                    margin: EdgeInsets.all(4),
                    height: 25,
                    child: FlutterMarquee(
                      autoStart: true,
                        children: <Widget>[
                          RichText(
                            text: TextSpan(
                              text: "恭喜会计小王子5分钟前获得",
                              style: TextStyle(color: Color(0xFF666666)),
                              children: [
                                TextSpan(
                                  text: "对啊书签",
                                  style: TextStyle(color: Color(0xFFEC2626)),
                                )
                              ]
                            ),
                          ),
                          RichText(
                            text: TextSpan(
                                text: "恭喜会计小王子5分钟前获得",
                                style: TextStyle(color: Color(0xFF666666)),
                                children: [
                                  TextSpan(
                                    text: "对啊书签",
                                    style: TextStyle(color: Color(0xFFEC2626)),
                                  )
                                ]
                            ),
                          ),
                          RichText(
                            text: TextSpan(
                                text: "恭喜会计小王子5分钟前获得",
                                style: TextStyle(color: Color(0xFF666666)),
                                children: [
                                  TextSpan(
                                    text: "对啊书签",
                                    style: TextStyle(color: Color(0xFFEC2626)),
                                  )
                                ]
                            ),
                          ),
                        ],
                        onChange: (i) {
                          print(i);
                        },
                        animationDirection: AnimationDirection.t2b,
                        duration: 5),
                  ),
                ],
              ),
            ),
            SizedBox(height: 20,),
            Container(
              child: Stack(
                alignment: Alignment.center,
                children: <Widget>[
                  RotationTransition(turns:Tween(begin: start,end: end).animate(CurvedAnimation(parent: controller, curve: Curves.fastOutSlowIn)),
                    child: Container(
                    width: 285,
                    height: 285,
                    child: TurntableWidget(),
                  ),),
                  Center(
                    child: InkWell(
                      child: Image.asset(ImageHelper.wrapAssets("activity_table_btn.png"),width: 100,height: 100,fit: BoxFit.cover,),
                      onTap: (){
                        if(controller.isAnimating){
                          controller.stop();
                        }else{
                          print("暂定");
//                          controller.reset();
                          controller.forward();
                        }

                      },
                    ),

                  )

                ],
              ),
            )

          ],
        ),
      ),
      width: double.infinity,
      height: 450,
      margin: EdgeInsets.only(left: 20, right: 20),
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    controller.dispose();
    super.dispose();
  }
}



